package com.hnit.applet.utils;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class UnitUtil {
	/**
	 * 吉焦/吨=2.99.
	 */
	private static final BigDecimal BILLION_JOULE_DIVIDE_TON_RATIO = new BigDecimal("2.99");

	/**
	 * @description 入库 调用
	 * @param originValue
	 * @param unitMapBack
	 *                   原始值，应当使用的单位Map（/通过unit-conversion/unit-map-back接口获得"）
	 * @return java.lang.String 目标值
	 */
	public static String toDataBaseEle(String originValue, Map<Integer, String> unitMapBack) {
		if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.ELE.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static String toDataBasePrice(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.PRICE.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static String toDataBaseFee(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.FEE.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static String toDataBaseCapacity(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.CAPACITY.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static String toDataBaseQuality(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.QUALITY.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static String toDataBaseCarbon(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.CARBON.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static BigDecimal toDataBaseEle(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.ELE.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static BigDecimal toDataBasePrice(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.PRICE.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static BigDecimal toDataBaseFee(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.FEE.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static BigDecimal toDataBaseCapacity(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.CAPACITY.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static BigDecimal toDataBaseQuality(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.QUALITY.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static BigDecimal toDataBaseCarbon(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.CARBON.getIndex());
		return calculateToDB(originValue, coef);
	}

	public static String calculateToDB(String originValue, String coef) {
//        if ("1".equals(coef)) return originValue; 需要去多余零
		if (originValue == null) {
			return null;
		} else if ("".equals(originValue)) {
			return "";
		} else if (new BigDecimal("0").compareTo(new BigDecimal(originValue)) == 0) {
			return "0";
		} else {
			return MathUtils.elegantMultiply(originValue, coef);
		}
	}

	public static BigDecimal calculateToDB(BigDecimal originValue, String coef) {
		if (originValue == null) {
			return null;
		} else if (originValue.compareTo(BigDecimal.ZERO) == 0) {
			return originValue;
		} else {
			return MathUtils.multiply(originValue, new BigDecimal(coef)).stripTrailingZeros();
		}
	}

	/**
	 * 转换为万千瓦时
	 * 
	 * @description
	 * @author 胡晓光
	 * @datetime 2022年3月21日 上午9:04:03
	 * @param originValue
	 * @return
	 */
	public static String fromDataBaseTo10000kWh(String originValue) {
		Map<Integer, String> unitMapBack = new HashMap<>();
		unitMapBack.put(1, "10");// 万千瓦时
		String coef = unitMapBack.get(UnitConstants.ELE.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static String fromDataBaseToYuan$kWh(String originValue) {
		Map<Integer, String> unitMapBack = new HashMap<>();
		unitMapBack.put(2, "1000");// 万千瓦时
		String coef = unitMapBack.get(UnitConstants.PRICE.getIndex());
		return calculateFromDB(originValue, coef);
	}

	/**
	 * @description 出库 调用
	 * @param originValue
     * @param unitMapBack
	 * @return java.lang.String
	 * @author bofei
	 * @date 2019/9/24 11:35
	 */
	public static String fromDataBaseEle(String originValue, Map<Integer, String> unitMapBack) {
		if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.ELE.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static String fromDataBasePrice(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.PRICE.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static String fromDataBaseFee(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.FEE.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static String fromDataBaseCapacity(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.CAPACITY.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static String fromDataBaseQuality(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.QUALITY.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static String fromDataBaseCarbon(String originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.CARBON.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static BigDecimal fromDataBaseEle(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.ELE.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static BigDecimal fromDataBasePrice(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.PRICE.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static BigDecimal fromDataBaseFee(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.FEE.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static BigDecimal fromDataBaseCapacity(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.CAPACITY.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static BigDecimal fromDataBaseQuality(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.QUALITY.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static BigDecimal fromDataBaseCarbon(BigDecimal originValue, Map<Integer, String> unitMapBack) {
        if (null == unitMapBack){
            return originValue;
        }
		String coef = unitMapBack.get(UnitConstants.CARBON.getIndex());
		return calculateFromDB(originValue, coef);
	}

	public static String calculateFromDB(String originValue, String coef) {
		boolean creatable = NumberUtils.isCreatable(originValue);
		if (!creatable) {
			return originValue;
		}
		if (originValue == null) {
			return null;
		} else if ("".equals(originValue)) {
			return "";
		} else if (new BigDecimal("0").compareTo(new BigDecimal(originValue)) == 0) {
			return "0";
		} else {
			return MathUtils.elegantDivide(originValue, coef, 10);
		}
	}

	public static BigDecimal calculateFromDB(BigDecimal originValue, String coef) {

		if (originValue == null) {
			return null;
		} else if (originValue.compareTo(BigDecimal.ZERO) == 0) {
			return originValue;
		} else {
			return MathUtils.divide(originValue, new BigDecimal(coef), 10).stripTrailingZeros();
		}
	}

	/**
	 * @title: 网站转换单位使用 -- 由千千瓦时-->万千瓦时
	 * @description: TODO
	 * @author: fengxx
	 * @date: 2019-11-05 11:03
	 * @param: list 要转换的数据
	 * @return: 转换后的数据
	 */
	public static List<Double> convertUnitbyList(List<Double> list) {
		List<Double> resList = new ArrayList<>();
		/**
		 * 单位转换系数 10
		 */
		Map<Integer, String> map = new HashMap();
		map.put(UnitConstants.ELE.getIndex(), "10");
		for (Double o : list) {
			resList.add(Double.valueOf(UnitUtil.fromDataBaseEle(o.toString(), map)));
		}
		return resList;
	}

	/**
	 * 吉焦转换为{@code number}吨(如:万吨).转换系数为{@link UnitUtil}的成员变量{@code BILLION_JOULE_DIVIDE_TON_RATIO}.
	 *
	 * @param billionJoule 吉焦
	 * @param numberTon    吨的倍数
	 * @param scale        保留的小数位数
	 * @return 吨. {code billionJoule}不能转换为数字时,返回 ""
	 */
	public static String convertBillionJouleToNumberTon(Object billionJoule, BigDecimal numberTon, int scale) {
		if (billionJoule == null) {
			return "";
		}
		String billionJouleStr = billionJoule.toString();
		if (NumberUtils.isParsable(billionJouleStr)) {
			return "";
		}
		return new BigDecimal(billionJouleStr).multiply(numberTon).divide(BILLION_JOULE_DIVIDE_TON_RATIO, scale, BigDecimal.ROUND_HALF_UP).toPlainString();
	}
}
